<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Linkman extends Model
{
    //联系人的添加与编辑
    public function addeditlink($data)
    {
        $ckdata = notEmpty(['name' => lang("name"), 'position' => lang("job")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        if (!isset($data['linkmethods'])) {
            return array('msg' => lang("contact_can't_be_empty"), 'status' => 2);
        }
        //没有公司的时候公司id是0
        if (!$data['co_id']) {
            $data['co_id'] = 0;
        }
        $infos = Db::name('company')->where('id', $data['co_id'])->value('status');
        if ($infos == 'LOCK') {
            return array('msg' => $data['name'] . lang("status_locked_can't_be_edit"), 'status' => 0);
        }
        //添加联系方式
        if ($data['act'] == 'liaisonadd') {
            $data['create_id'] = $data['update_id'] = session('userid');
            $data['create_at'] = $data['update_at'] = time();
            //将数据写入到联系人表
            Db::startTrans();
            //一个公司只用一个联系方式是主要的账号
            if ($data['is_main'] == 1) {
                //将之前的主账号变成非主要
                $this->where('co_id', $data['co_id'])->setField('is_main', 0);
            }
            $linkmanid = $this->strict(false)->insertGetId($data);//公司属性表
            $datamethod['linkman_id'] = $linkmanid;//联系人id
            $datamethod['linkmeths'] = $data['linkmethods'];//联系方式
            //将数据写入到联系方式表
            $linkmethid = $this->addlinkmeth($datamethod);
            if ($linkmethid['status'] == 0) {
                return array('msg' => $linkmethid['msg'], 'status' => 0);
                exit;
            }
            if ($linkmanid && $linkmethid['status'] == 1) {
                Db::commit();
                return array('msg' => $data['name'] . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'liaisonedit') {
            //一个公司只用一个联系方式是主要的账号
            if ($data['is_main'] == 1) {
                //将之前的主账号变成非主要
                $this->where('co_id', $data['co_id'])->setField('is_main', 0);
            }
            $updata['update_id'] = session('userid');
            $updata['update_at'] = time();
            $updata['co_id'] = $data['co_id'];
            $updata['name'] = $data['name'];
            $updata['position'] = $data['position'];
            $updata['is_main'] = $data['is_main'];
            $updata['sex'] = $data['sex'];
            Db::startTrans();
            $linkmanid = $this->where('id', $data['linkid'])->update($updata);//公司属性表
            //将数据写入到联系方式表
            $datamethod['linkman_id'] = $data['linkid'];//联系人id
            $datamethod['linkmeths'] = $data['linkmethods'];//联系方式
            $linkmethid = $this->addlinkmeth($datamethod);
            if ($linkmethid['status'] == 0) {
                return array('msg' => $linkmethid['msg'], 'status' => 0);
                exit;
            }
            if ($linkmanid !== false && $linkmethid['status'] == 1) {
                Db::commit();
                return array('msg' => $data['name'] . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $data['name'] . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //联系方式的写入
    protected function addlinkmeth($datamethod)
    {
        //删除这个联系人的之前的联系方式
        Db::name('linkman_method')->where('linkman_id', $datamethod['linkman_id'])->delete();
        //判断用户手机号和邮箱是否符合要求
        foreach ($datamethod['linkmeths'] as $keys => $vals) {
            if ($vals['coattr_id'] == '177') {
                //邮箱
                if (!pregMAIL($vals['content'])) {
                    return array('msg' => lang("wrong_email_format"), 'status' => 0);
                    exit;
                }
            } elseif ($vals['coattr_id'] == '176') {
                //手机号
                if (!pregPN($vals['content'])) {
                    return array('msg' => lang("wrong_mobile_number_format"), 'status' => 0);
                    exit;
                }
            }
        }

        $count = count($datamethod['linkmeths']);//提交的联系方式的个数
        $linkmethods = [];
        if ($count) {
            for ($i = 0; $i < $count; $i++) {
                $row = [
                    'linkman_id' => $datamethod['linkman_id'],
                    'create_id' => session('userid'),
                    'update_id' => session('userid'),
                    'create_at' => time(),
                    'update_at' => time(),
                    'content' => $datamethod['linkmeths'][$i]['content'],
                    'coattr_id' => $datamethod['linkmeths'][$i]['coattr_id'],
                ];
                $linkmethods[] = $row;
            }
        }
        $reslinkmanid = Db::name('linkman_method')->insertAll($linkmethods);
        if ($reslinkmanid) {
            return array('msg' => '', 'status' => 1);
            exit;
        } else {
            return array('msg' => '', 'status' => 0);
            exit;
        }
    }

    //联系人的删除
    public function dellink($data)
    {
        $dliname = getinfo($data['linkid'], 'linkman')['name'];
        Db::startTrans();
        $retlink = $this->where('id', $data['linkid'])->delete();//联系人表
        $linkmeth = Db::name('linkman_method')->where('linkman_id', $data['linkid'])->delete();
        if ($retlink && $linkmeth) {
            Db::commit();
            return array('msg' => $dliname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $dliname . lang("failed_to_delete"), 'status' => 0);
        }
    }
}